package com.example.gkcircle;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVOSCloud;
import com.example.gkcircle.utils.MyAlertView;
import com.example.gkcircle.utils.MySVProgressHUD;

import java.util.zip.Inflater;

/**
 * 所有Fragment基类
 * Created by Zhouhua on 15/12/19.
 */
public abstract class BaseFragment extends Fragment {

    private RelativeLayout mBarBg;
    private TextView mTitle;
    private ImageButton mLeftBtn;
    private ImageButton mRightBtn;
    private View self;
    public boolean dataFlag;//防止用户中途退出空指针异常

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (this.self == null) {
            this.self = inflater.inflate(setLayout(), null);
        }
        if (this.self.getParent() != null) {
            ViewGroup parent = (ViewGroup) this.self.getParent();
            parent.removeView(this.self);
        }
        mTitle = (TextView) self.findViewById(R.id.tabbar_title);
        mLeftBtn = (ImageButton) self.findViewById(R.id.tabbar_left_btn);
        mRightBtn = (ImageButton) self.findViewById(R.id.tabbar_right_btn);
        mBarBg=(RelativeLayout)self.findViewById(R.id.tabbar_bg);
        initView(self);
        initListener();
        setData();
        return self;
    }

    protected abstract int setLayout();

    protected abstract void initView(View view);

    protected abstract void initListener();

    protected abstract void setData();


    public void setActionBarTitle(String title) {
        mTitle.setText(title);
    }

    public void setLeftBtnBackgound(int id) {
        mLeftBtn.setBackgroundResource(id);
    }

    public void setRightBtnBackgound(int id) {
        mRightBtn.setBackgroundResource(id);
    }
    public void setbarBackgoundColor(int id) {
        mBarBg.setBackgroundColor(id);
    }


    public void setmTitleHide(Boolean hide) {
        if (hide) mTitle.setVisibility(View.GONE);
        else mTitle.setVisibility(View.VISIBLE);
    }

    public void setmLeftBtnHide(Boolean hide) {
        if (hide) mLeftBtn.setVisibility(View.GONE);
        else mLeftBtn.setVisibility(View.VISIBLE);
    }

    public void setmRighttBtnHide(Boolean hide) {
        if (hide) mRightBtn.setVisibility(View.GONE);
        else mRightBtn.setVisibility(View.VISIBLE);
    }

    @Override
    public void onResume() {
        super.onResume();
        dataFlag=true;
    }

    @Override
    public void onStop() {
        super.onStop();
        dataFlag=false;
        if (MySVProgressHUD.mSVProgressHUD != null) {
            MySVProgressHUD.mSVProgressHUD = null;
        }
        if(MyAlertView.alertView!=null){
            MyAlertView.alertView=null;
        }
    }

}